package yl.hs.bmipfull.controllers.laboratorydata;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.laboratorydata.ILabExperimentDefinitionLibsService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:   <br/>
 * 创建时间: 2022-06-20 10:32 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "实验检测标准——标准库", tags = "实验检测标准——标准库")
@RequestMapping(value = "/api/lab/definitionLibs")
@RestController
public class LabExperimentDefinitionLibsController extends ControllerBase {
    @Autowired
    ILabExperimentDefinitionLibsService service;
    @ApiOperation(value = "查询标准库信息", notes = "查询标准库信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询标准库信息", notes = "查询标准库信息")
    @RequestMapping(value = "/queryall", method = RequestMethod.GET)
    public WebResult queryList() {
        try {
            return success("查询成功", service.queryList(new PageQuery()));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询标准库信息", notes = "分页查询标准库信息")
    @RequestMapping(value = {"/query/{rowNum}/{page}","/{rowNum}/{page}/{productCateSn}"}, method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "保存标准库信息", notes = "查询/更新标准库信息")
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveLabExperimentDefinitionlibs(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "删除标准库信息", notes = "删除标准库信息")
    @RequestMapping(value = "/delete/{sn}", method = RequestMethod.GET)
    public WebResult deleteData(@ApiParam(value = "编号") @PathVariable String sn){
        Integer result;
        try {
            result = service.UpdateStateBySN (sn,-1);
            if (result > 0) {
                return success("操作成功");
            } else {
                return fail("操作失败");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "删除标准库信息", notes = "删除标准库信息")
    @RequestMapping(value = "/change/{sn}/{state}", method = RequestMethod.GET)
    public WebResult changeData(@ApiParam(value = "编号") @PathVariable String sn,@ApiParam(value = "数据状态") @PathVariable int state){
        Integer result;
        try {
            result = service.UpdateStateBySN (sn,state);
            if (result > 0) {
                return success("操作成功");
            } else {
                return fail("操作失败");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
}
